#!/usr/bin/php
<?PHP

//交换两个整型变量的值
function swapInt()
{
    $a = 5;
    $b = 3;

    echo '$a = ', $a, ', $b = ', $b, PHP_EOL;

    /*
     * $c = $a;
     * $a = $b;
     * $b = $c;
     */
    /*
     * a = a + b;
     * b = a - b;
     * a = a - b;
     */

/*
 *     101
 *     011
 *     110
 *     $a = $a ^ $b; 6
 *
 *     110
 *     011
 *     101
 *     $b = $b ^ $a; 5
 *
 *     110
 *     101
 *     011
 *     $a = $a ^ $b; 3
 */

    $a ^= $b;
    $b ^= $a;
    $a ^= $b;

    echo 'swaps $a = ', $a, ', $b = ', $b, PHP_EOL;
}

//将某个数的某个位置1或置0
function bitOp()
{
    printf("请输入一个整型: ");
    fscanf(STDIN, "%d", $num);

    // 1101 1001   0xd9 217
    // 1110 0110   0xe6
    //  bit从0开始
    // 把某位置0  此位为0的数与此数位与 num & ~(1 << bit)
    // 把某位置1  此位为1的数与此数位或 num | (1 << bit)
    /*
     * 111...1111 0111
     *     ~(1 << 4)
     *     ~(1 | 1 << 3 | 1 << 4)
     *     ~(1 | 3 << 3)
     *     ~1 & 0xc9
     */

    $num &= ~(1 | (3 << 3));
    $num |= (3 << 1 | 1 << 5);

    printf("num = 0x%x\n", $num);
}

//转换大小写
function uTol()
{
    for (;;) {
        printf("请输入一个字符: ");
        $ch = ord(fgets(STDIN));

        if ($ch == ord('q'))
            break;

        $l = ord("a");
        $u = ord("A");

        /*
         * if ($ch >= $l && $ch < $l + 26
         *         || $ch >= $u && $ch <= $u + 26))
         * {
         *     if ($ch >= $l)
         *         $ch -= 32;
         *     else
         *         $ch += 32;
         * }
         */

        $ch = ($ch >= $l && $ch < $l + 26
                || $ch >= $u && $ch <= $u + 26) ?
            ($ch >= $l ? $ch - 32 : $ch + 32) : $ch;

        printf("ch = %c\n", $ch);
    }
}

//9x9乘法表
function mul99()
{
    for ($i = 1; $i < 10; ++$i) {
        for ($j = 1; $j <= $i; ++$j) {
            printf( "%d x %d = %-2d ", $j, $i, $i * $j);
        }
        echo PHP_EOL;
    }
}

//打印直角三角形
function triangle()
{
    printf("请输入边长: ");
    fscanf(STDIN, "%d", $n);

    /*
        * * *
        # * *
        # # *
     */

    for ($i = 0, $t = $n * $n; $i < $t; ++$i) {
        $r = (int)($i / $n);
        // $r = intdiv($i, $n);  //php7
        $c = $i % $n;

        // echo "r = ", $r, ", c = ", $c, "\n";

        if ($c >= $r) {
            echo ' * ';
        } else {
            echo '   ';
        }
        if ($c == $n - 1) {
            echo "\n";
        }
    }

    echo "\n";

    for ($i = 0; $i < $n; ++$i) {
        for ($j = 0; $j < $i; ++$j) {
            echo "   ";
        }
        for ($j = 0, $t = $n - $i; $j < $t; ++$j) {
            echo " * ";
        }
        echo "\n";
    }
}

//黑色星期五
function blackFri()
{
    $year = 2017;

    for ($i = 1900, $days = 0; $i < $year; $i++) {
        $days += 365;
        if ($i % 400 == 0 || $i % 4 == 0 && $i % 100 != 0)
            $days += 1;
    }
    $days += 13;

    $isyear = false;

    if ($year % 400 == 0 || $year % 4 == 0 && $year % 100 != 0)
        $isyear = true;

    for ($i = 1; $i <= 12; $i++) {
        /*
         * 1       0
         * 2       1
         * 3       1
         * 4       2
         * 5       2
         * 6       3
         * 7       3
         * 8       4
         * 9       5
         * 10      5
         * 11      6
         * 12      6
         */
        $d = $days + ($i - 1) * 30 + intdiv($i, 2);
        if ($i == 9 || $i == 11)
            $d += 1;
        if ($i > 2) {
            $d -= 2;
            if ($isyear)
                $d += 1;
        }

        if ($d % 7 == 5)
            printf("%4d 年 %2d 月 13号是黑色星期五!\n", $year, $i);
    }
}

//输出1 - 1/2 + 1/3 ... + 1/99 - 1/100的结果
function addSub()
{
    // php: 整型相除结果为浮点型

    for ($i = 2, $s = 1; $i <= 100; $i++) {
        if ($i % 2 == 0)
            $s -= 1 / $i;
        else
            $s += 1 / $i;
    }
    printf("s = %.2f\n", $s);
}

//八进制转十进制
function octToDec()
{
    printf("请输入整型(八进制): ");
    fscanf(STDIN, "%d",  $num);

    // 153   1 * 8^2 + 5 * 8^1 + 3 * 8^0
    /*
     * num / 1 % 10
     * num / 10 % 10
     * num / 100 % 10
     * num / 1000 % 10
     */

    // for ($n = $num; $n > 0; $n = (intdiv)($n, 10))
    for ($n = $num, $b = 0, $s = 0; $n > 0; $n = (int)($n / 10), ++$b) {
        $m = $n % 10;
        if ($m > 7) {
            fwrite(STDERR, "请输入合法的八进制!\n");
            return;
        }

        for ($j = 0, $t = 1; $j < $b; ++$j, $t *= 8)
            ;
        $s += $m * $t;
    }

    echo 's = ', $s, PHP_EOL;
}

//求二进制中有多少个1
function binOne()
{
    printf("请输入整型: ");
    fscanf(STDIN, "%d",  $num);

    for ($i = 0, $c = 0; $i < 64; $i++) {
        if ($num & (1 << $i))
            $c++;
    }
    echo $num, " 此数二进制中有 ", $c, " 个1\n";
}

//买鸡的组合
function buyChicken()
{
    $g = 100 / 5;
    $m = 100 / 3;
/*
    for ($i = 0; $i < $g; ++$i) {
        for ($j = 0; $j < $m; ++$j) {
            for ($k = 0; $k < 100; ++$k) {
                if ($k % 3 == 0
                        && $i + $j + $k == 100
                        && $i * 5 + $j * 3 + $k / 3 == 100)
                    printf("公鸡: %d只, 母鸡: %d只, 小鸡: %d只\n", $i, $j, $k);
            }
        }

    }
*/
    for ($i = 0; $i < $g; ++$i) {
        for ($j = 0; $j < $m; ++$j) {
            $k = 100 - $i - $j;
            if ($k % 3 == 0 && $i * 5 + $j * 3 + $k / 3 == 100)
                printf("公鸡: %d只, 母鸡: %d只, 小鸡: %d只\n", $i, $j, $k);
        }
    }
}

// swapInt();
// bitOp();
// uTol();
// mul99();
// triangle();
// blackFri();
// addSub();
// octToDec();
// binOne();
buyChicken();

